home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / kwmv.arc / CROOT.C next >
C/C++ Source or Header  |  1985-08-08  |  4KB  |  181 lines

  1. /* Copyright (C) 1981,1982, 1983 by Manx Software Systems */
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #ifndef NULL
  5. #define NULL ((void *)0)
  6. #endif
  7.  
  8. char *get_first(), *get_next(), *sbrk();
  9.  
  10. #define ARGMAX 256
  11. static char *Argv[ARGMAX];
  12. static int argvsize;
  13. static int Argc;
  14. static char curr_path[128];
  15.  
  16. noper()
  17. {
  18.     return 0;
  19. }
  20.  
  21. int (*cls_)() = noper;
  22. extern char _ioflg[];
  23.  
  24. Croot(cp, first)
  25. register char *cp;
  26. {
  27.     register char *cp2;
  28.     char *save;
  29.     char *wild_match;
  30.     char *index(),*rindex(), *save_str();
  31.     char *path,*copy; int j;
  32.  
  33.     _ioflg[0] = isatty(0);    /* set flag for i/o routines */
  34.     _ioflg[1] = isatty(1);    /* set flag for i/o routines */
  35.     _ioflg[2] = isatty(2);    /* set flag for i/o routines */
  36.  
  37.  
  38.     /* Null out first argument */
  39.     Argv[0] = "";
  40.     Argc = first;
  41.  
  42.     /* loop through arguments */
  43.     for (;;) 
  44.     {
  45.         /* skip blanks */
  46.         while (*cp == ' ' || *cp == '\t')
  47.             ++cp;
  48.  
  49.         /* if you're at the end of command line, you're done */
  50.         if (*cp == 0)
  51.             break;
  52.  
  53.         /* find beginning of next argument */
  54.         cp2 = cp;    /* save original pointer to the string */
  55.         *cp2 = (*cp2 == '/' ? '\\' : *cp2);
  56.         while (*++cp2)
  57.         {
  58.             /* if you hit a space char - stick a null in to terminate last
  59.                argument
  60.              */
  61.             if (*cp2 == ' ' || *cp2 == '\t') 
  62.             {
  63.                 *cp2++ = 0;
  64.                 break;
  65.             }
  66.             *cp2 = (*cp2 == '/' ? '\\' : *cp2);
  67.         }
  68.  
  69.         /* if no wild card characters, do it the old fashioned way */
  70.         if (index(cp,'*') == NULL && index(cp,'?') == NULL)
  71.         {
  72.             /* update the next argv pointer */
  73.             Argv[Argc] = cp;
  74.             /* bump the argument count */
  75.             if (++Argc == ARGMAX)
  76.                 abort();
  77.         }
  78.         else
  79.         {
  80.             /* if there is a path included, save it off */
  81.             if ((path = rindex(cp,'\\')) || (path = rindex(cp,'/')))
  82.             {
  83.                 copy = cp;
  84.                 /* copy to curr_path, mapping / to \ */
  85.                 for (j = 0; j < sizeof(curr_path) && copy != path+1; copy++,j++)
  86.                     curr_path[j] = (*copy == '/' ? '\\' : *copy);
  87.                 /* terminate string */
  88.                 curr_path[j] = '\0';
  89.             }
  90.             else
  91.             /* null path */
  92.                 curr_path[0] = 0;
  93.             if (wild_match = get_first(cp))
  94.             {
  95.                 /* update the next argv pointer */
  96.                 Argv[Argc]= save_str(wild_match);
  97.                 /* bump the argument count */
  98.                 if (++Argc == ARGMAX)
  99.                     abort();
  100.                 /* get the rest of the matching file names */
  101.                 while (wild_match = get_next())
  102.                 {
  103.                     
  104.                     /* update the next argv pointer */
  105.                     Argv[Argc] = save_str(wild_match);
  106.                     /* bump the argument count */
  107.                     if (++Argc == ARGMAX)
  108.                         abort();
  109.                 }
  110.             }
  111.         }
  112.         cp = cp2;    /* point to beginning of next argument */
  113.     }
  114.     Argv[Argc] = NULL;    
  115.     main(Argc,Argv);
  116.     exit(0);
  117. }
  118.  
  119. char *save_str(s)
  120.     register char *s;
  121. {
  122.     register char *r;
  123.     int pathlen;
  124.     /* squirrel away matched file name */
  125.     if (NULL == (r = sbrk(strlen(s)+(pathlen = strlen(curr_path))+1)))
  126.         abort();
  127.     strcat(curr_path,s);
  128.     strcpy(r,curr_path);
  129.     curr_path[pathlen] = '\0';
  130.     return r;
  131. }
  132.  
  133. abort()
  134. {
  135.     write(2, "Too many args.", 14);
  136.     _exit(200);
  137. }
  138.  
  139. exit(code)
  140. {
  141.     (*cls_)();
  142.     _exit(code);
  143. }
  144.  
  145. typedef struct
  146. {
  147.     char dos_reserved[21];
  148.     char attribute;
  149.     unsigned file_time;
  150.     unsigned file_date;
  151.     long file_size;
  152.     char file_name[13];
  153. } fcb;
  154. fcb wildcard;
  155.  
  156. char *get_first(fname)
  157.     char *fname;
  158. {
  159.     register int result;
  160.     /* set the disk transfer address */
  161.     bdos(0x1A,&wildcard);
  162.     result = bdos(0x4E,fname,0);
  163.     /* make the find first call */
  164.     if(2 == result || 18 == result)
  165.         return NULL;
  166.     return &(wildcard.file_name[0]);
  167. }
  168.  
  169. char *get_next(fname)
  170.     char *fname;
  171. {
  172.     register int result;
  173.     /* set the disk transfer address */
  174.     bdos(0x1A,&wildcard);
  175.     result = bdos(0x4f,0,0);
  176.     /* make the find next call */
  177.     if (18 == result)
  178.         return NULL;
  179.     return &(wildcard.file_name[0]);
  180. }
  181.